package com.task.scheduling.job;

import com.task.scheduling.constants.Constants;
import com.task.scheduling.dao.TaskSchedulingDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;

/**
 * @program: task-scheduling
 * @description: 使执行失败的job重新执行，状态为4
 * @author: zhouliangliang
 * @create: 2018-11-14 14:34
 **/
public class ReviveJob implements Runnable{

    private Logger log = LoggerFactory.getLogger(ReviveJob.class);

    private static volatile boolean flag = true;
    private TaskSchedulingDao taskSchedulingDao;

    public ReviveJob(TaskSchedulingDao taskSchedulingDao) {
        this.taskSchedulingDao = taskSchedulingDao;
    }

    /**
     * 被复活的任务与新加入任务的区别
     * 1. 执行次数大于0次
     */
    @Override
    public void run() {
        while (flag) {
            int num = taskSchedulingDao.reviveTask(new Date());
            log.info(num + "个执行失败的任务被复活");
            try {
                Thread.sleep(Constants.reviveTaskInterval);
            } catch (InterruptedException e) {
                log.info("ReviveJob sleep exception!" + e.getMessage(), e);
            }
        }
    }
}
